<template>
  <div class="app-container">
    <Ht-search-form
      v-show="showSearch"
      :dictMap="dictMapAsync"
      :queryParams="queryParams"
      :outputQueryParams="queryParams"
      :formOptions="formOptions"
      :advOptions="[]"
      :labelWidth="75"
    ></Ht-search-form>

    <!--    <div class="page-label-block">-->
    <!--      <span class="color-block">|</span>-->
    <!--      <span class="page-label-name">-->
    <!--      租户管理-->
    <!--      </span>-->
    <!--    </div>-->

    <ht-table
      ref="dataTable"
      :multi-select="false"
      :showSerialNumber="false"
      :dictMap="dictMap"
      :queryParams="queryParams"
      :tableBindColumns="tableBindColumns"
      :opButtonList="opButtonList"
      :reqConfig="regConfig"
    >
      <template v-slot:operate>
        <el-table-column label="操作" align="left" width="50" fixed="right">
          <template slot-scope="scope">
            <ht-button-group
              :buttonList="[
                {
                  label: '明细',
                  doFunc: () => {
                    handleAction('detail', scope.row);
                  },
                },
              ]"
            />
          </template>
        </el-table-column>
      </template>
    </ht-table>

    <!-- 添加或修改弹出框 -->
    <el-dialog :title="title" center :visible.sync="open" width="1024px" append-to-body :close-on-click-modal="false">
      <el-form ref="detailForm" :model="detailForm" :rules="rules" label-width="120px">
        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="交易流水号" prop="id">
              <el-input v-model="detailForm.id" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="协议号" prop="proto">
              <el-input v-model="detailForm.proto" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="CPG日期" prop="workdate">
              <el-date-picker
                v-model="detailForm.workdate"
                style="width: 100%"
                type="date"
                value-format="yyyyMMdd"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="协议类型" prop="protoType">
              <el-select
                v-model="detailForm.protoType"
                clearable
                placeholder="协议类型"
                style="width: 100%"
                @change="protoTypeChange"
              >
                <el-option
                  v-for="dict in dictMap.PROTOCOL_TYPE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接收行" prop="rcvbrnoType">
              <el-select
                v-model="detailForm.rcvbrnoType"
                clearable
                placeholder="接收行"
                style="width: 100%"
                @change="rcvbrnoTypeChange"
              >
                <el-option
                  v-for="dict in dictMap.RCVBRNO_TYPE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="接收行行号" prop="rcvBrno">
              <el-input v-model="detailForm.rcvBrno" @change="checkbankcode('rcvBrno')" :disabled="rcvBrnoDisable" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="20px">
              <el-button
                icon="el-icon-search"
                plain
                type="primary"
                :style="{ display: visibleHiden }"
                @click="openBankBranchDialog('rcvBrno')"
              >
                行名行号查询
              </el-button>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="接收行行名" prop="rcvBrnoname">
              <el-input v-model="detailForm.rcvBrnoname" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="付款人账号" prop="payerActno">
              <el-input v-model="detailForm.payerActno" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="付款人名称" prop="payerName">
              <el-input v-model="detailForm.payerName" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="付款人开户行行号" prop="payerAccBrno">
              <el-input v-model="detailForm.payerAccBrno" @change="checkbankcode('payerAccBrno')" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="20px">
              <el-button icon="el-icon-search" plain type="primary" @click="openBankBranchDialog('payerAccBrno')">
                行名行号查询
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="付款人开户行行名" prop="payerAccBrnoname">
              <el-input v-model="detailForm.payerAccBrnoname" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="付款行行号" prop="payerBrno">
              <el-input v-model="detailForm.payerBrno" @change="checkbankcode('payerBrno')" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="20px">
              <el-button icon="el-icon-search" plain type="primary" @click="openBankBranchDialog('payerBrno')">
                行名行号查询
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="付款行行名" prop="payerBrnoname">
              <el-input v-model="detailForm.payerBrnoname" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="付款人清算行行号" prop="payerStBrno">
              <el-input v-model="detailForm.payerStBrno" @change="checkbankcode('payerStBrno')" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="20px">
              <el-button icon="el-icon-search" plain type="primary" @click="openBankBranchDialog('payerStBrno')">
                行名行号查询
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="付款人清算行行名" prop="payerStBrnoname">
              <el-input v-model="detailForm.payerStBrnoname" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="收款人名称" prop="payeeName">
              <el-input v-model="detailForm.payeeName" :disabled="payeeNameDisable" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="录入操作员" prop="mdTlrno">
              <el-input v-model="detailForm.mdTlrno" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="录入时间" prop="mdTime">
              <el-date-picker
                style="width: 100%"
                v-model="detailForm.mdTime"
                value-format="yyyyMMddHHmmss"
                type="datetime"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="24">
            <el-form-item label="审核附言" prop="audRemarks">
              <el-input type="textarea" v-model="detailForm.audRemarks" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="审核操作员" prop="audTlrno">
              <el-input v-model="detailForm.audTlrno" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核时间" prop="audTime">
              <el-date-picker
                style="width: 100%"
                v-model="detailForm.audTime"
                value-format="yyyyMMddHHmmss"
                type="datetime"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="save" v-loading.fullscreen.lock="loading">提 交</el-button>
        <el-button type="primary" @click="del" v-loading.fullscreen.lock="loading">删 除</el-button>
        <el-button type="primary" @click="cancel">关 闭</el-button>
      </div>
      <bank-branch-dialog ref="bankBranchDialog" @confirm="confirmBankBranch"></bank-branch-dialog>
    </el-dialog>

    <agent-pay-in-out-sign-entry ref="agentPayInOutSignEntry" :dictMap="dictMap" @editComplete="editComplete"></agent-pay-in-out-sign-entry>

  </div>
</template>

<script>
import { listPageMixin } from "@/libs/listPageMixin";
import {
  AgentPayInOutModifyDelete,
  AgentPayInOutModifyDetail,
  AgentPayInOutModifySave,
  apiUrlConfigModify,
} from "@/api/cnaps/beps/392";
import { getBankInfByBankCode } from "@/api/cnaps/common/bankInfo";
import BankBranchDialog from "@/views/components/BankBranchDialog";
import AgentPayInOutSignEntry from "@/views/cnaps/beps/392/agent_pay_in_out_sign_entry";
export default {
  name: "AgentPayInOutModify",
  mixins: [listPageMixin],
  components: { AgentPayInOutSignEntry, BankBranchDialog },
  data() {
    return {
      // 查询参数
      queryParams: {
        idSeqNo:'',
        proto:'',
        startWorkdate:'',
        endWorkdate:'',
      },
      loading: false,
      // 搜索
      formOptions: [],

      opButtonList: () => {
        return [
          {
            label: "新增交易",
            type: "primary",
            clickFun: () => {
              this.transAdd();
            },
          },
        ];
      },

      //
      searchFormDictList: ["TYPE392", "PROTOCOL_TYPE", "RCVBRNO_TYPE"],
      // 列信息
      regConfig: apiUrlConfigModify,
      tableBindColumns: [
        { prop: "id", label: "交易流水号", width: 180 },
        { prop: "workdate2", label: "工作日期", type: "date", width: 120 },
        { prop: "workdate", label: "CPG日期", type: "date", width: 120 },
        { prop: "proto", label: "协议号", width: 120 },
        { prop: "payerName", label: "付款人名称", width: 120 },
        { prop: "payeeName", label: "收款人名称", width: 120 },
        { prop: "mdTlrno", label: "录入操作员", width: 120 },
        { prop: "mdTime", label: "录入时间", type: "date", width: 180 },
        { prop: "audTlrno", label: "审核操作员", width: 120 },
        { prop: "audTime", label: "审核时间", type: "date" },
      ],
      dictMapAsync: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      detailForm: {
        proto: undefined,
        protoType: undefined,
        rcvbrnoType: undefined,
        rcvBrno: undefined,
        rcvBrnoname: undefined,
        payerActno: undefined,
        payerName: undefined,
        payerAccBrno: undefined,
        payerAccBrnoname: undefined,
        payerBrno: undefined,
        payerBrnoname: undefined,
        payerStBrno: undefined,
        payerStBrnoname: undefined,
        payeeName: undefined,
      },
      totalRecord: 0,
      totalPage: 0,
      needCount: 0,
      payeeNameDisable:false,
      rcvBrnoDisable:false,
      visibleHiden:'',
      rules: {
        id: [{ max: 20, message: "最大为20位", trigger: "blur" }],
        proto: [{ max: 32, message: "长度最大为32位", trigger: "blur" }],
        rcvbrnoType: [{ required: true, message: "请输入接收行", trigger: "blur" }],
        rcvBrno: [
          { required: true, message: "请输入接收行行号", trigger: "blur" },
          { max: 14, message: "长度最大为14位", trigger: "blur" },
        ],
        rcvBrnoname: [{ required: true, message: "请输入接收行行名", trigger: "blur" }],
        payerActno: [
          { required: true, message: "请输入付款人账号", trigger: "blur" },
          { max: 32, message: "长度最大为32位", trigger: "blur" },
        ],
        payerName: [
          { required: true, message: "请输入付款人名称", trigger: "blur" },
          { max: 60, message: "长度最大为60位", trigger: "blur" },
        ],
        payerAccBrno: [{ max: 14, message: "长度最大为14位", trigger: "blur" }],
        payerAccBrnoname: [{ max: 60, message: "长度最大为60位", trigger: "blur" }],
        payerBrno: [
          { required: true, message: "请输入付款行行号", trigger: "blur" },
          { max: 14, message: "长度最大为14位", trigger: "blur" },
        ],
        payerBrnoname: [
          { required: true, message: "请输入付款行行名", trigger: "blur" },
          { max: 60, message: "长度最大为60位", trigger: "blur" },
        ],
        payerStBrno: [{ max: 14, message: "长度最大为14位", trigger: "blur" }],
        payerStBrnoname: [{ max: 60, message: "长度最大为60位", trigger: "blur" }],
        payeeName: [
          { required: true, message: "请输入收款人名称", trigger: "blur" },
          { max: 60, message: "长度最大为60位", trigger: "blur" },
        ],
        audRemarks: [{ max: 128, message: "长度最大为128位", trigger: "blur" }],
      },
    };
  },
  mounted() {
    const formOptions = [
      // 普通搜索

      {
        type: "input",
        prop: "idSeqNo",
        defaultAttr: {
          size: "small",
          label: "交易流水号",
          placeholder: "请输入交易流水号",
        },
      },

      {
        type: "input",
        prop: "proto",
        defaultAttr: {
          size: "small",
          label: "协议号",
          placeholder: "请输入协议号",
        },
      },

      {
        type: "dates",
        nameObj: {
          startTimeName: "startWorkdate",
          endTimeName: "endWorkdate",
        },
        defaultAttr: {
          size: "small",
          label: "工作日期",
          placeholder: ["请输入工作开始日期", "请输入工作结束日期"],
          format: "yyyy-MM-dd",
          valueFormat: "yyyyMMdd",
        },
      },
    ];
    this.formOptions = formOptions;
    this.dictMapAsync = this.getDictMap();
  },
  methods: {
    /***
     * 按钮操作汇总
     */
    /***
     * 按钮操作汇总
     */
    handleAction(type, row = {}) {
      switch (type) {
        case "detail":
          this.handleDetail(row);
          break;
      }
    },

    // 关闭按钮
    cancel() {
      this.open = false;
    },

    /** 详情按钮操作 */
    handleDetail(row) {
      this.title = "代收付协议修改明细";
      AgentPayInOutModifyDetail(row).then((response) => {
        this.detailForm = response.result.data;
        this.$set(this.detailForm, "rcv_brno", response.result.rcv_brno);
        this.open = true;
      });
    },
    //点击保存按钮
    save: function () {
      this.$refs["detailForm"].validate((valid) => {
        if (valid) {
          this.loading = true;
          AgentPayInOutModifySave(this.detailForm)
            .then((response) => {
              this.msgSuccess("提交成功");
              this.open = false;
              this.loading = false;
              this.$nextTick(() => {
                this.$refs.dataTable.doQuery(true);
              });
            })
            .catch((err) => {
              this.loading = false;
            });
        }
      });
    },

    //点击删除按钮
    del() {
      this.$refs["detailForm"].validate((valid) => {
        if (valid) {
          this.loading = true;
          AgentPayInOutModifyDelete(this.detailForm)
            .then((response) => {
              this.msgSuccess("删除成功");
              this.open = false;
              this.loading = false;
              this.$nextTick(() => {
                this.$refs.dataTable.doQuery(true);
              });
            })
            .catch((err) => {
              this.loading = false;
            });
        }
      });
    },

    //行名行号返回
    confirmBankBranch(fromFlag, row) {
      if (fromFlag === "payerAccBrno") {
        this.$set(this.detailForm, "payerAccBrno", row.bankCode);
        this.$set(this.detailForm, "payerAccBrnoname", row.orgFullname);
      } else if (fromFlag === "payerBrno") {
        this.$set(this.detailForm, "payerBrno", row.bankCode);
        this.$set(this.detailForm, "payerBrnoname", row.orgFullname);
      } else if (fromFlag === "payerStBrno") {
        this.$set(this.detailForm, "payerStBrno", row.bankCode);
        this.$set(this.detailForm, "payerStBrnoname", row.orgFullname);
      } else if (fromFlag === "rcvBrno") {
        this.$set(this.detailForm, "rcvBrno", row.bankCode);
        this.$set(this.detailForm, "rcvBrnoname", row.orgFullname);
      }
    },
    //行名行号查询
    openBankBranchDialog(fromFlag = "") {
      this.$refs.bankBranchDialog.show(fromFlag);
    },

    checkbankcode(fromFlag) {
      if (fromFlag === "payerAccBrno") {
        let payerAccBrno = this.detailForm.payerAccBrno;
        getBankInfByBankCode(payerAccBrno)
          .then((response) => {
            if (response.code == "S" && response.result !== null && response.result !== undefined ) {
              this.$set(this.detailForm, "payerAccBrno", response.result.bankCode);
              this.$set(this.detailForm, "payerAccBrnoname", response.result.orgFullname);
            } else {
              this.msgError("无行名行号记录");
              this.$set(this.detailForm, "payerAccBrno", null);
              this.$set(this.detailForm, "payerAccBrnoname", null);
            }
          })
          .catch((err) => {
            this.$set(this.detailForm, "payerAccBrno", null);
            this.$set(this.detailForm, "payerAccBrnoname", null);
          });
      } else if (fromFlag === "payerBrno") {
        let payerBrno = this.detailForm.payerBrno;
        getBankInfByBankCode(payerBrno)
          .then((response) => {
            if (response.code == "S" && response.result !== null && response.result !== undefined) {
              this.$set(this.detailForm, "payerBrno", response.result.bankCode);
              this.$set(this.detailForm, "payerBrnoname", response.result.orgFullname);
            } else {
              this.msgError("无行名行号记录");
              this.$set(this.detailForm, "payerBrno", null);
              this.$set(this.detailForm, "payerBrnoname", null);
            }
          })
          .catch((err) => {
            this.$set(this.detailForm, "payerBrno", null);
            this.$set(this.detailForm, "payerBrnoname", null);
          });
      } else if (fromFlag === "payerStBrno") {
        let payerStBrno = this.detailForm.payerStBrno;
        getBankInfByBankCode(payerStBrno)
          .then((response) => {
            if (response.code == "S" && response.result !== null && response.result !== undefined) {
              this.$set(this.detailForm, "payerStBrno", response.result.bankCode);
              this.$set(this.detailForm, "payerStBrnoname", response.result.orgFullname);
            } else {
              this.msgError("无行名行号记录");
              this.$set(this.detailForm, "payerStBrno", null);
              this.$set(this.detailForm, "payerStBrnoname", null);
            }
          })
          .catch((err) => {
            this.$set(this.detailForm, "payerStBrno", null);
            this.$set(this.detailForm, "payerStBrnoname", null);
          });
      } else if (fromFlag === "rcvBrno") {
        let rcvBrno = this.detailForm.rcvBrno;
        getBankInfByBankCode(rcvBrno)
          .then((response) => {
            if (response.code == "S" && response.result !== null && response.result !== undefined) {
              this.$set(this.detailForm, "rcvBrno", response.result.bankCode);
              this.$set(this.detailForm, "rcvBrnoname", response.result.orgFullname);
            } else {
              this.msgError("无行名行号记录");
              this.$set(this.detailForm, "rcvBrno", null);
              this.$set(this.detailForm, "rcvBrnoname", null);
            }
          })
          .catch((err) => {
            this.$set(this.detailForm, "rcvBrno", null);
            this.$set(this.detailForm, "rcvBrnoname", null);
          });
      }
    },

    protoTypeChange() {
      let protoType = this.detailForm.protoType;
      if (protoType === "CT01") {
        this.payeeNameDisable = true;
        this.$set(this.detailForm, "payeeName", undefined);
        this.$set(this.rules.payeeName[0], "required", false);
      } else {
        this.payeeNameDisable = false;
        this.$set(this.rules.payeeName[0], "required", true);
      }
    },

    rcvbrnoTypeChange() {
      //todo 需要代付中心行号
      let rcvbrnoType = this.detailForm.rcvbrnoType;
      if (rcvbrnoType === "0") {
        this.$set(this.detailForm, "rcvBrno", this.detailForm.rcv_brno);
        this.checkbankcode("rcvBrno");
        this.visibleHiden = "none";
        this.rcvBrnoDisable = true;
      } else {
        this.$set(this.detailForm, "rcvBrno", undefined);
        this.$set(this.detailForm, "rcvBrnoname", undefined);
        this.visibleHiden = "";
        this.rcvBrnoDisable = false;
      }
    },

    /** 新增按钮操作 */
    transAdd() {
      this.$refs.agentPayInOutSignEntry.show();
    },
  },
};
</script>
